package ce.com.cenewbluesdk.scan;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;

/**
 * Created by Administrator on 2016/11/15 0015.
 */
public class CEScanDev_3 extends CEScanDevBase {

    private static  final String name="CEScanDev_3";

    private BluetoothAdapter mAdapter;
    private boolean haveRegisterBroad = false;
    Handler handler = new Handler();

    IntentFilter filter;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case BluetoothDevice.ACTION_FOUND:
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, (short) 0);
                    findDev(device, rssi, null);
                    break;
                case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                    break;
                case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
                    break;
                default:
                    break;
            }
        }
    };


    public CEScanDev_3(Context context, FindBlueTooth findDev) {
        super(context, findDev);
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

    }

    @Override
    public void startScan() {
        synchronized (name) {
            if (!haveRegisterBroad) {
                haveRegisterBroad = true;
                try {
                    mContext.registerReceiver(mReceiver, filter);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            handler.removeCallbacks(stopScanRun);
            handler.postDelayed(stopScanRun, 12000);
            mAdapter.cancelDiscovery();
            mAdapter.startDiscovery();
            for (BluetoothDevice device : mAdapter.getBondedDevices()) {
                findDev(device, -1, null);
            }
        }
    }

    @Override
    public void stopScan() {
        synchronized (name) {
            if (haveRegisterBroad) {
                haveRegisterBroad = false;
                try {
                    mContext.unregisterReceiver(mReceiver);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            mAdapter.cancelDiscovery();
        }


    }

    @Override
    public boolean isBleEnabled() {
        if (mAdapter == null)
            return false;
        return mAdapter.isEnabled();
    }

    StopScanRun stopScanRun = new StopScanRun();

    class StopScanRun implements Runnable {
        @Override
        public void run() {
            stopScan();
        }
    }

}
